package cn.edu.scau.cmi.zhangjunming.client;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;

import cn.edu.scau.cmi.zhangjunming.adapter.*;

public class AdapterClient extends Application{
    
	boolean isClassAdapter = true;
	List<Student> stuL = new ArrayList<Student>();
	List<Integer> intL = new ArrayList<Integer>();
	
	
	public static void main(String[]args) {
		launch(args);
	}
	
	public AdapterClient() {
		
		
	}

	public void start(Stage primaryStage) throws Exception {
		
		stuL.clear();
		
		//输入窗口
		TextField numField = new TextField();
		numField.setPrefWidth(100);
		numField.setLayoutX(50);
		numField.setLayoutY(42);
		
		TextField nameField = new TextField();
		nameField.setPrefWidth(100);
		nameField.setLayoutX(220);
		nameField.setLayoutY(42);
		
		// 显示信息的窗口
		TextArea textArea = new TextArea();
		textArea.setPrefWidth(600);
		textArea.setPrefHeight(400);
		textArea.setLayoutX(10);
		textArea.setLayoutY(80);
		
		Label numLabel = new Label("学号：");
		numLabel.setPrefWidth(80);
		numLabel.setLayoutX(10);
		numLabel.setLayoutY(45);
		
		Label nameLabel = new Label("姓名：");
		nameLabel.setPrefWidth(80);
		nameLabel.setLayoutX(180);
		nameLabel.setLayoutY(45);
		
		Label modLabel = new Label("适配器：类适配器");
		modLabel.setPrefWidth(150);
		modLabel.setLayoutX(10);
		modLabel.setLayoutY(12);

		// 模式改变按钮
		Button changeButton = new Button("改变模式");
		changeButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent arg0) {
				if (isClassAdapter) {
					modLabel.setText("适配器：对象适配器");
				} else {
					modLabel.setText("适配器：类适配器");
				}
				isClassAdapter = !isClassAdapter;
			}
		});
		changeButton.setLayoutX(160);
		changeButton.setLayoutY(8);

		//退出按钮
		Button returnButton = new Button("退出");
		returnButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent arg0) {
				Stage stage = (Stage)returnButton.getScene().getWindow();
				stage.close();
			}
		});
		returnButton.setLayoutX(260);
		returnButton.setLayoutY(8);
		returnButton.setPrefWidth(80);
		
		Button createButton = new Button("录入学生信息");
		createButton.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				if(numField.getText()!=null&&nameField.getText()!=null) {
					//Student newStu = new Student();
					//stuL.add(newStu);
					//stuL.add(stuL.size(), newStu);
					stuL.add(new Student(Integer.valueOf(numField.getText()),nameField.getText()));
					intL.add(Integer.valueOf(numField.getText()));
				}
				if(isClassAdapter) {
					ClassAdapter cAdapter = new ClassAdapter();
					
					cAdapter.sortStudent(intL, stuL);
					textArea.clear();
					
					for (int i = 0; i < cAdapter.stuList.size(); i++) {
						textArea.appendText(String.valueOf(cAdapter.stuList.get(i).getNum()) + " "
								+ cAdapter.stuList.get(i).getName()+"\n");
					}

				} else {
					ObjectAdapter oAdapter = new ObjectAdapter();
					oAdapter.sortStudent(intL, stuL);
					textArea.clear();

					for (int i = 0; i < oAdapter.stuList.size(); i++) {
						textArea.appendText(String.valueOf(oAdapter.stuList.get(i).getNum()) + " "
								+ oAdapter.stuList.get(i).getName()+"\n");
					}
				}
				
			}
		});
		createButton.setPrefWidth(150);
		createButton.setLayoutX(350);
		createButton.setLayoutY(41);
		
		Group group=new Group(createButton,changeButton,returnButton,numField,nameField,textArea,numLabel,nameLabel,modLabel);
		Scene scene = new Scene(group,620,550);
		
		primaryStage.setTitle("适配器");
		
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}
